package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.pojo.entity.EwsRole;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import com.egoo.ticket.server.service.DepartmentService;
import com.egoo.ticket.server.service.RoleService;
import com.egoo.ticket.server.service.UserService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Donny_dong
 */
@RestController
@RequestMapping(value = "/v1/role", produces = "application/json")
@Api(value = "角色管理",tags = "角色管理",description = "角色管理")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private UserService userService;

	@Autowired
	private DepartmentService departmentService;

	@ApiOperation(value = "获取所有的可见范围", notes = "获取所有的可见范围")
	@RequestMapping(method = RequestMethod.GET, value="/selectAll")
	public DbResponse selectAll(@ApiParam(value="可见类型", required=true) @RequestParam("type") String type,
								HttpServletRequest request){
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		if(type.equals(ConstantCode.UserRoleDeptType.ROLE)){
			List<InteractiveDataVo> roleList =
					roleService.getRolesInteractiveByCondition(new HashMap<>(4),tenantId);
			dbResponse.setData(roleList);
		}else if (type.equals(ConstantCode.UserRoleDeptType.USER)){
			Map<String, Object> searchMap = new HashMap<>(2);
			searchMap.put("tenantId",tenantId);
			List<InteractiveDataVo> userList = userService.getInteractiveUsers(searchMap);
			dbResponse.setData(userList);
		}else if (type.equals(ConstantCode.UserRoleDeptType.DEPT)){
			List<InteractiveDataVo> departmentTreeData = departmentService.getDepartmentTreeData(tenantId);
			dbResponse.setData(departmentTreeData);
		}
		dbResponse.setRetCode(200);

		return dbResponse;
	}

	@ApiOperation(value = "模糊查询获取所有的角色", notes = "模糊查询获取所有的角色")
	@RequestMapping(method = RequestMethod.GET, value="/selectAllRole")
	public DbResponse selectAllRole(@ApiParam(value="角色名称", required=true) @RequestParam("name") String name,
									HttpServletRequest request){
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		Map<String, Object> stringObjectHashMap = new HashMap<>(4);
		stringObjectHashMap.put("name",name);
		stringObjectHashMap.put("tenantId",tenantId);
		List<EwsRole> roleList =  roleService.getRolesListByCondition(stringObjectHashMap);
         dbResponse.setRetCode(200);
         dbResponse.setData(roleList);
		return  dbResponse;
	}

	@ApiOperation(value = "模糊查询获取所有的用户", notes = "模糊查询获取所有的用户")
	@RequestMapping(method = RequestMethod.GET, value="/selectAllUser")
	public DbResponse selectAllUser(@ApiParam(value="用户名称", required=true) @RequestParam("name") String name,
									HttpServletRequest request){
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		Map<String, Object> stringObjectHashMap = new HashMap<>(4);
		stringObjectHashMap.put("name",name);
		stringObjectHashMap.put("tenantId",tenantId);
		List<EwsUser> userList =  userService.getAllUserList(stringObjectHashMap);
		dbResponse.setRetCode(200);
		dbResponse.setData(userList);
		return  dbResponse;
	}



	@ApiOperation(value = "获取所有的可见类型", notes = "获取所有的可见类型")
	@RequestMapping(method = RequestMethod.GET, value="/selectAllMenuVisibleType")
	public DbResponse selectAllMenuVisibleType(){

		List<InteractiveDataVo> visibleList = new ArrayList<>();
		InteractiveDataVo interactiveData = new InteractiveDataVo();
		interactiveData.setLabel(ConstantCode.UserRoleDeptType.ALL_ZH);
		interactiveData.setValue(ConstantCode.UserRoleDeptType.ALL);
		visibleList.add(interactiveData);
		// 组装可见范围下拉数据
		List list = handleVisibleData(visibleList);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,list);
	}

	@ApiOperation(value = "获取所有的类型", notes = "获取所有的类型")
	@RequestMapping(method = RequestMethod.GET, value="/selectAllType")
	public DbResponse selectAllType(){

		List visibleList = new ArrayList<>();
		List list = handleVisibleData(visibleList);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,list);
	}
	/**
	 * TODO 处理可见范围下拉数据
	 * @author: victor_tang
	 * @createtime: 2020/2/4 12:07
	 * @param visibleList 1
	 * @return: java.utils.List
	 */
	public List handleVisibleData(List visibleList){
		// 部门
		InteractiveDataVo interactiveData1 = new InteractiveDataVo();
		interactiveData1.setLabel(ConstantCode.UserRoleDeptType.DEPT_ZH);
		interactiveData1.setValue(ConstantCode.UserRoleDeptType.DEPT);
		visibleList.add(interactiveData1);
		// 角色
		InteractiveDataVo interactiveData2 =  new InteractiveDataVo();
		interactiveData2.setLabel(ConstantCode.UserRoleDeptType.ROLE_ZH);
		interactiveData2.setValue(ConstantCode.UserRoleDeptType.ROLE);
		visibleList.add(interactiveData2);
		// 用户
		InteractiveDataVo interactiveData3 = new InteractiveDataVo();
		interactiveData3.setLabel(ConstantCode.UserRoleDeptType.USER_ZH);
		interactiveData3.setValue(ConstantCode.UserRoleDeptType.USER);
		visibleList.add(interactiveData3);

		return visibleList;
	}

}
